/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import ru.vsu.domain.Bid;
import ru.vsu.domain.Lot;
import ru.vsu.domain.Thing;
import ru.vsu.service.LotConstants;
import ru.vsu.service.LotFinisherWatchDog;
import ru.vsu.service.LotService;

/**
 *  Бин детальной информации о предмете, позволяющий выставить его на аукцион.
 *
 * @author Roman Velichkin <vrnroman@gmail.com>
 */
@Named(value="thingBean")
@SessionScoped
public class ThingBean implements Serializable {
    
    @EJB
    private LotService lotService;

    @Inject
    private transient MainPage mainPage;

    @NotNull
    private Thing thing;

    private Lot lot;

    private Integer startPrice = 0;

    private Integer newPrice = new Integer(0);

    private String message;

    /**
     * Счётчик, показывающий, через сколько секунд лот будет продан,
     * если не сделать ставку.
     */
    private int backCounter;

    /**
     * Является ли залогиненный человек хозяином предмета.
     * @return
     */
    public boolean isOwner() {
        if (getThing() == null || mainPage.getUserId() == null) {
            return false;
        }
        return getThing().getOwner().getId().equals(mainPage.getUserId());
    }


    public boolean isThingOnSale() {
        if (getThing() == null) {
            return false;
        }
        return lotService.isThingOnSale(getThing());
    }

    public void doNewBid() {
        if (lotService.addNewBid(lot, newPrice, mainPage.getUserId())) {
            //lotFinisherWatchDog.addNewEventForLot(lot.getId());
            backCounter = LotConstants.SECONDS_WAITING_FOR_BID;
            message = "Ваша ставка успешно добавлена";
            return;
        }
        message = "Добавление данной ставки невозможно";
    }

    public List<Bid> getBidsForLot() {
        if (lot == null) {
            return new ArrayList<Bid>();
        }
        int prevBidsCount = lot.getBids().size();
        lot = lotService.updateLot(lot.getId());
        int newBidsCount = lot.getBids().size();
        if (newBidsCount > prevBidsCount) {
            backCounter = LotConstants.SECONDS_WAITING_FOR_BID;
        }
        return lot.getBids();
    }

    public String tryToSell() {
        lot = lotService.createLotForThing(getThing(), getStartPrice());
        return null;
    }

    public void fakeListener() {
        if (backCounter > 0) {
            --backCounter;
        }
        message = "";
    }

    public Lot getLot() {
        return lot;
    }

    public void setLot(Lot lot) {
        this.lot = lot;
    }

    public Integer getStartPrice() {
        return startPrice;
    }

    public void setStartPrice(Integer startPrice) {
        this.startPrice = startPrice;
    }


    public Thing getThing() {
        if (thing == null && lot != null) {
            thing = lot.getThing();
        }
        return thing;
    }

    public void setThing(Thing thing) {
        lot = null;
        this.thing = thing;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getNewPrice() {
        return newPrice;
    }

    public void setNewPrice(Integer newPrice) {
        this.newPrice = newPrice;
    }

    public int getBackCounter() {        
        return backCounter;
    }

    public void setBackCounter(int backCounter) {
        this.backCounter = backCounter;
    }



    

    /** Creates a new instance of ThingBean */
    public ThingBean() {
    }

}
